<template>
  <div class="stuList">
    <table>
      <thead>
        <tr>
          <th>学生头像</th>
          <th>学生姓名</th>
          <th>学生年龄</th>
          <th>学生性别</th>
          <th>学生班级</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for='item in studentData.students' :key='item._id'>
          <td>
            <!-- 动态绑定src 地址 -->
            <img style='width:50px;height: 50px;border-radius:100px;' :src=" 'http://localhost:3000/img/' + ( item.imagesName || 'defaultImg.jpg') " alt="stu-logo">
          </td>
          <td>{{item.name}}</td>
          <td>{{item.age}}</td>
          <td>{{item.gender}}</td>
          <td>{{item.classId.name}}</td>
          <td>
            <button @click='$emit("transStudent",item)'>修改</button>
            <button @click='deleteStudent(item._id)'>删除</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  props:{
    studentData:{
      type:Object,
      default: ()=>({})
    }
  },
  mounted(){
    //挂载完成就调用
    this.$emit('getStudents');
  },
  methods:{
    async deleteStudent(_id){
      const data = await axios({
        url:'/students/deleteStudent',
        method:'get',
        params:{
          _id
        }
      }).then(
        ({data} = data) =>{
          if (data.status) {
            alert(data.message);
            this.$emit('getStudents');
          }else{
            alert(data.message);
          }
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
